import config from '../config'
const nameSpace = config.namespace

export default {
    getAll() {
        return JSON.parse(localStorage.getItem(nameSpace) || '{}')
    },
    get(key: string) {
        const storage = this.getAll()
        return storage[key]
    },
    set(key: string, value: any) {
        const storage = this.getAll()
        storage[key] = value
        localStorage.setItem(nameSpace, JSON.stringify(storage))
    },
    del(key: string) {
        const temp = this.getAll()
        delete temp[key]
        localStorage.setItem(nameSpace, JSON.stringify(temp))
    },
    clear() {
        localStorage.setItem(nameSpace, '')
    },
    setState(key: string, val: string) {
        const state = this.get('state') || {}
        state[key] = val
        this.set('state', state)
    },
    getState(key: string) {
        const state = this.get('state')
        if (state && state[key]) return state[key]
        return ''
    },
}
